package com.netease.yunxin.app.im.service

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import com.netease.yunxin.app.im.utils.ForegroundNotificationHelper

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/5/25 12:01
 * ModifyTime: 12:01
 * Description:
 */
class SqueezeService : Service() {
    val helper by lazy { ForegroundNotificationHelper() }
    var isRunning = false

    // Binder given to clients
    private val binder = LocalBinder()

    companion object {
        val TAG = "ProjectionService"
    }

    override fun onCreate() {
        Log.i(TAG, "[onCreate]")
        helper.attach(this)
        super.onCreate()
    }

    override fun onStart(intent: Intent?, startId: Int) {
        super.onStart(intent, startId)
        Log.i(TAG, "[onStart]")
        helper.attach(this)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.i(TAG, "[onStartCommand]")
        isRunning = true
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onUnbind(intent: Intent?): Boolean {
        Log.i(TAG, "onUnbind")
        helper.deleteForegroundNotification()
        return super.onUnbind(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "[onDestroy]")
        helper.deleteForegroundNotification()
        isRunning = false
    }

    override fun onBind(intent: Intent?): IBinder? {
        helper.attach(this)
        return binder
    }

    inner class LocalBinder : Binder() {
        // Return this instance of PlayerService so we can call public methods
        fun getService(): SqueezeService = this@SqueezeService
    }

}